package core;
import org.kxml2.kdom.*; 

public class DayOfWeek{
	
	private int m_dayNumber;
	private static final String m_days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", 
							"Friday", "Saturday", "Sunday"};

	public DayOfWeek(int numberOfDay){
		setDay(numberOfDay);
	}
	
	public void setDay(int numberOfDay){
		if(numberOfDay >= m_days.length)
			m_dayNumber = m_days.length - 1;
		else if(numberOfDay < 0)
			m_dayNumber = 0;
		else 
			m_dayNumber = numberOfDay;
	}
	
	public String getDayName(){
		return m_days[m_dayNumber];
	}
	
	public int getDayNumber(){
		return m_dayNumber;
	}
	
	public boolean equals(Object other) {
		DayOfWeek otherDay = (DayOfWeek)other;
		return m_dayNumber == otherDay.m_dayNumber;		
	}
	
	public static String[] dayNames(){
		return m_days;
	}
	
	public void toXml(Node node){
		Element dowNode = node.createElement(null, "DayOfWeek");
		dowNode.setAttribute(null, "number", new Integer(m_dayNumber).toString());
		node.addChild(Node.ELEMENT, dowNode);        
	}

	public static DayOfWeek fromXml(Element elem) throws Throwable{
		try
	    {
			Element rootElem = elem; 
			if (!rootElem.getName().equals("DayOfWeek")){
				rootElem = elem.getElement(null, "DayOfWeek");
			}
			
			int num = Integer.parseInt(rootElem.getAttributeValue(null, "number"));
			return new DayOfWeek(num);
	    }
		catch (Exception e) {
			throw e;
		}
	}
	
}
